package fr.poc.rest.job;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.discovery.DiscoveryManager;

@Component
//@EnableScheduling
public class ScheduledJerseyAppli2Client {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	private static final DynamicPropertyFactory configInstance = com.netflix.config.DynamicPropertyFactory
			.getInstance();

	@Value("${target.base.url}")
	private String targetBaseUrl;

	private Client client = ClientBuilder.newClient();

	@Scheduled(initialDelay = 30000, fixedDelay = 10000)
	public void callHello() {
		// String uri = targetBaseUrl + "/hello";

		// eureka
		String vipAddress = configInstance.getStringProperty(
				"eureka.vipAddressTarget", "pocserviceB.mydomain.net").get();
		InstanceInfo nextServerInfo = DiscoveryManager.getInstance()
				.getDiscoveryClient()
				.getNextServerFromEureka(vipAddress, false);

		String uri = "http://" + nextServerInfo.getHostName() + ":"
				+ nextServerInfo.getPort() + "/rest/hello";

		logger.info(">>callHello>>>>vipAdress={} , uri={}", vipAddress, uri);

		WebTarget target = client.target(uri);
		String greetings = target.request(MediaType.APPLICATION_JSON_TYPE).get(
				String.class);

		logger.info(">>callHello>>>> URI:{} / REPONSE:{}", uri, greetings);

	}

	@Scheduled(initialDelay = 45000, fixedDelay = 10000)
	public void callMultiply() {
		// String uri = targetBaseUrl + "/multiply";
		Double op1 = Math.random() * 100;
		Double op2 = Math.sqrt(Math.random() * 100) * 1.45;

		// eureka
		String vipAddress = configInstance.getStringProperty(
				"eureka.vipAddressTarget", "pocserviceB.mydomain.net").get();
		InstanceInfo nextServerInfo = DiscoveryManager.getInstance()
				.getDiscoveryClient()
				.getNextServerFromEureka(vipAddress, false);

		String uri = "http://" + nextServerInfo.getHostName() + ":"
				+ nextServerInfo.getPort() + "/rest/multiply";

		logger.info(">>callMultiply>>>>vipAdress={} , uri={}", vipAddress, uri);

		WebTarget target = client.target(uri);
		Double response = target.path(op1.toString()).path(op2.toString())
				.request(MediaType.APPLICATION_JSON_TYPE)
				.accept(MediaType.APPLICATION_JSON).post(null, Double.class);

		logger.info(">>callMultiply>>>> URI:{} / REPONSE:{}", uri, response);

	}

	@Scheduled(initialDelay = 40000, fixedDelay = 10000)
	public void callConcat() {
		String op1 = "ray";
		String op2 = "mon";
		// String uri = targetBaseUrl + "/concat";

		// eureka
		String vipAddress = configInstance.getStringProperty(
				"eureka.vipAddressTarget", "pocserviceB.mydomain.net").get();
		InstanceInfo nextServerInfo = DiscoveryManager.getInstance()
				.getDiscoveryClient()
				.getNextServerFromEureka(vipAddress, false);

		String uri = "http://" + nextServerInfo.getHostName() + ":"
				+ nextServerInfo.getPort() + "/rest/concat";

		WebTarget target = client.target(uri);
		String response = null;
		try {
			response = target.path(op1).path(op2)
					.request(MediaType.APPLICATION_JSON_TYPE)
					.accept(MediaType.APPLICATION_JSON_TYPE)
					.post(null, String.class);

		} catch (Exception e) {
			logger.error("error in callConcat ", e);
		} finally {
			logger.info(">>callConcat>>>> URI:{} / REPONSE:{}", uri, response);
		}
	}

}
